/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

struct VS_UNDERWATER
{
	float4 Position: POSITION0;
	float4 Color : COLOR0;
	float2 TexCoord: TEXCOORD0;
};

struct PS_UNDERWATER
{
	float4 Position: POSITION0;
	float4 Color : COLOR0;
	float2 TexCoord0: TEXCOORD0;
	float2 TexCoord1: TEXCOORD1;
};

#ifdef VERTEXSHADER
#include "vsconstants.h"

PS_UNDERWATER UnderwaterVS (VS_UNDERWATER vs_in)
{
	PS_UNDERWATER vs_out;

	vs_out.Position = mul (WorldMatrix, vs_in.Position);
	vs_out.Color = vs_in.Color;
	vs_out.TexCoord0 = vs_in.TexCoord;
	vs_out.TexCoord1 = vs_in.TexCoord * WarpScale + float2 (WarpSpeed, -WarpSpeed);

	return vs_out;
}
#endif


#ifdef PIXELSHADER
#include "psconstants.h"

#define drawSampler tmu0Sampler
#define warpSampler tmu1Sampler
#define waterSampler tmu2Sampler

float4 UnderwaterPS (PS_UNDERWATER ps_in) : COLOR0
{
	// read the noise image
	float4 warp = tex2D (warpSampler, ps_in.TexCoord1);

	// perform the warp
	float4 warpx = float4 (1.0, -0.5, -1.0, 0.5) * WarpFactor;
	float4 warpy = float4 (-0.5, 1.0, 0.5, -1.0) * WarpFactor;
	float2 warpcoord = float2 (dot (warpx, warp), dot (warpy, warp));

	// sample from the smallest miplevel of the specified liquid texture to find the average colour to blend with
	float3 waterFactor = tex2Dlod (waterSampler, float4 (0.0f, 0.0f, 0.0f, 666.0f)).rgb;

	// scale the shift up to match the colour intensity of the the original software shift
	float4 waterBlend = float4 (waterFactor * (ShadeLight.y / dot (waterFactor, float3 (0.3f, 0.59f, 0.11f))), saturate (ShadeLight.x));

	// run the warp effect and blend in the other polyblend colours (if any)
	float4 otherBlend = lerp (tex2D (drawSampler, ps_in.TexCoord0 + warpcoord), ps_in.Color, ps_in.Color.a);

	// and blend the two together for the final output
	return lerp (otherBlend, waterBlend, waterBlend.a);
}
#endif

